package cn.edu.ctbu.kcsj.webapi;

import cn.edu.ctbu.kcsj.core.PageUtils;
import cn.edu.ctbu.kcsj.domain.Course;
import cn.edu.ctbu.kcsj.domain.Score;
import cn.edu.ctbu.kcsj.service.CourseService;
import cn.edu.ctbu.kcsj.service.ScoreService;
import cn.edu.ctbu.kcsj.utils.RUtil;
import cn.edu.ctbu.kcsj.vo.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

@RestController
@Slf4j
@RequestMapping("/webapi/score")
public class ScoreRestController {
    @Autowired
    private  ScoreService scoreService;
    @Autowired
    private CourseService courseService;
    @GetMapping("/getbypageteacher")
    public PageUtils getByPageteacher(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name,
                               @RequestParam(value = "farm",defaultValue = "") String farm,HttpServletRequest httprequest){
        Sort sort=Sort.by(Sort.Direction.ASC,"sno");
        Page<Score>scorePage;
        HttpSession session=httprequest.getSession();
        Integer tno=(Integer) session.getAttribute("userid");
        Course course=courseService.getByTno(tno);
        String cname=course.getCname();
        if (StringUtils.isEmpty(name)){
            Score score=new Score();
            score.setCname(cname);
            Pageable pageable=PageRequest.of(page,size,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("cname",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Score>example=Example.of(score,matcher);
            scorePage=scoreService.findAll(example,pageable);
        }else{
            Score score=new Score();
            score.setSname(name);
            score.setCname(cname);
            Pageable pageable=PageRequest.of(page,size,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("sname",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Score>example=Example.of(score,matcher);
            scorePage=scoreService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(scorePage.getContent(),scorePage.getTotalElements());
        return pageUtils;
    }
    @GetMapping("/getbypagestudent")
    public PageUtils getByPagestudent(@RequestParam(value = "page",defaultValue = "0") Integer page,
                                      @RequestParam(value = "size",defaultValue = "10") Integer size,
                                      @RequestParam(value = "name",defaultValue = "") String name,
                                      @RequestParam(value = "farm",defaultValue = "") String farm, HttpServletRequest httprequest){
        HttpSession session=httprequest.getSession();
        Integer sno=(Integer) session.getAttribute("userid");
        Sort sort=Sort.by(Sort.Direction.ASC,"cid");
        Pageable pageable=PageRequest.of(page,size,sort);
        Page<Score>scorePage;
        if (StringUtils.isEmpty(name)){
            Score score=new Score();
            score.setSno(sno);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("sno",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Score>example=Example.of(score,matcher);
            scorePage=scoreService.findAll(example,pageable);
        }else{
            Score score=new Score();
            score.setSno(sno);
            score.setCname(name);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("cname",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Score>example=Example.of(score,matcher);
            scorePage=scoreService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(scorePage.getContent(),scorePage.getTotalElements());
        return pageUtils;
    }
    /**
     * 新增成绩方法 /webapi/score/insert
     * @param score
     * @return
     */
    @PostMapping("/insert")
    public R insert(@Valid Score score, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errormsg=bindingResult.getFieldError().getDefaultMessage();
            log.info("新增成绩时出现错误！{}",errormsg);
            return RUtil.error(-10,errormsg);
        }
        scoreService.insert(score);
        return RUtil.success();
    }
    /**
     * 更新成绩方法 /webapi/score/update
     * @param score
     * @return
     */
    @PutMapping("/update")
    public Score update(Score score){
        score=scoreService.update(score);
        return score;
    }
    @GetMapping("/get/{sid}")
    public Score get(@PathVariable Integer sid){
        Score score=scoreService.getBySid(sid);
        return score;
    }
    @DeleteMapping("/delete/{sid}")
    public void delete(@PathVariable Integer sid){
        scoreService.delete(sid);
    }
    @GetMapping("/getname")
    public Course getname(HttpServletRequest httprequest){
        HttpSession session=httprequest.getSession();
        Integer tno=(Integer) session.getAttribute("userid");
        Course course=courseService.getByTno(tno);
        return course;
    }
}
